#include "homepage.h"

#include "ui_homepage.h"
#include <QTimerEvent>

HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);

    serialPort = new QSerialPort(this);

    ui->SlectMenu_level_2->move(0,660);
    Level2Menu_show = new QPropertyAnimation(ui->SlectMenu_level_2,"geometry");
    Level2Menu_show->setEasingCurve(QEasingCurve::InOutSine);
    Level2Menu_show->setDuration(200);


    ui->DropNumSetPageButton->setEnabled(false);
    ui->DropTypePageButton->setEnabled(false);

    MaxNum_of_TrayType_1=100;
    MaxNum_of_TrayType_2=25;
    MaxNum_of_TrayType_3=25;
    id1=startTimer(50);//每50ms调用一次
    SerialPortInit();
}

//定时器事件
void HomePage::timerEvent(QTimerEvent *e)
{
    if(e->timerId()==id1){
    ui->lcdNumber_dropnum->display(ui->TrayShow->DropNum);
        //修改
    ui->lcdNumber_droparea->display(ui->TrayShow->DropArea);
    }


}

HomePage::~HomePage()
{
    delete ui;
}


void HomePage::on_HomePage_StartButton_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->Start_SettingBoardTypePage);
}

void HomePage::on_HomePage_SetButton_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->StackedWidget_SettingPage);
}

void HomePage::on_SettingPage_ReturnButton_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->StackedWidget_HomePage);
}

void HomePage::on_StartPage_NextButton_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->StackedWidget_StartPage_2);
}

void HomePage::on_StartPage_ReturnButton_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->StackedWidget_HomePage);
}

void HomePage::on_StartPage2_ReturnButton_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->StackedWidget_StartPage);
}

//选择版型按键1，2，3。
void HomePage::on_SelectTray1_Button_clicked()
{
    ui->Tray->SetTrayType(1);
    ui->DripNum_spinBox->setMaximum(MaxNum_of_TrayType_1);
    if_DropNumSetPageButton_checkable();

    level_2_menu_show_hide();
}

void HomePage::on_SelectTray2_Button_clicked()
{
    ui->Tray->SetTrayType(2);
    ui->DripNum_spinBox->setMaximum(MaxNum_of_TrayType_2);
    if_DropNumSetPageButton_checkable();
    level_2_menu_show_hide();
}

void HomePage::on_SelectTray3_Button_clicked()
{
    ui->Tray->SetTrayType(3);
    ui->DripNum_spinBox->setMaximum(MaxNum_of_TrayType_3);
    if_DropNumSetPageButton_checkable();
    level_2_menu_show_hide();
}

//设置滴加数量数值框
void HomePage::on_DripNum_spinBox_valueChanged(int arg1)
{
    ui->Tray->SetDripNum(arg1);
    if_DropTypePageButton_checkable();
}

//选择液滴类型按键1，2，3。
void HomePage::on_SetDripNumType_1_Button_clicked()
{
    ui->Tray->SetDropType(1);

    level_2_menu_show_hide();

}

void HomePage::on_SetDripNumType_2_Button_clicked()
{
    ui->Tray->SetDropType(2);

    level_2_menu_show_hide();

}

void HomePage::on_SetDripNumType_3_Button_clicked()
{
    ui->Tray->SetDropType(3);

    level_2_menu_show_hide();

}

//测试输出
void HomePage::on_stackedWidget_currentChanged(int arg1)
{
    qDebug() << "test"<<arg1;
}

//一级菜单按键
void HomePage::on_TrayTypePageButton_clicked()
{
    ui->SlectMenu_level_2->setCurrentIndex(0);
    level_2_menu_show_hide();
}

void HomePage::on_DropNumSetPageButton_clicked()
{
    ui->SlectMenu_level_2->setCurrentIndex(1);
    level_2_menu_show_hide();
}

void HomePage::on_DropTypePageButton_clicked()
{
    ui->SlectMenu_level_2->setCurrentIndex(2);
    level_2_menu_show_hide();
}

//一级菜单按键是否可用
void HomePage::if_DropNumSetPageButton_checkable()
{
    if(ui->Tray->Tray_Type!=0)
    {
       ui->DropNumSetPageButton->setEnabled(true);
    }
}

void HomePage::if_DropTypePageButton_checkable()
{
    if(ui->DripNum_spinBox->value() ==0)
    {
       ui->DropTypePageButton->setEnabled(false);
    }
    else ui->DropTypePageButton->setEnabled(true);
}

//二级菜单伸出与隐藏
void HomePage::level_2_menu_show_hide()
{
    if (!Level2Menu_flag)
    {
        Level2Menu_show->setStartValue(QRect(0,ui->SlectMenu_level_2->pos().y(),ui->SlectMenu_level_2->width(),ui->SlectMenu_level_2->height()));
        Level2Menu_show->setEndValue(QRect(0,500,ui->SlectMenu_level_2->width(),ui->SlectMenu_level_2->height()));
        Level2Menu_show->start();
        Level2Menu_flag = !Level2Menu_flag;
    }
    else
    {
        Level2Menu_show->setStartValue(QRect(0,ui->SlectMenu_level_2->pos().y(),ui->SlectMenu_level_2->width(),ui->SlectMenu_level_2->height()));
        Level2Menu_show->setEndValue(QRect(0,700,ui->SlectMenu_level_2->width(),ui->SlectMenu_level_2->height()));
        Level2Menu_show->start();
        Level2Menu_flag = !Level2Menu_flag;
    }
}



void HomePage::SerialPortInit()
{
    QString port="ttymxc2";

    serialPort->setPortName(port);
    serialPort->setBaudRate(115200);
    serialPort->setDataBits(QSerialPort::Data8);
    serialPort->setParity(QSerialPort::EvenParity);
    serialPort->setStopBits(QSerialPort::OneStop);
    serialPort->setFlowControl(QSerialPort::NoFlowControl);

    serialPort->open(QIODevice::ReadWrite);
}













void HomePage::on_Button_DropNum_Add_clicked()
{
    int num;
    if(ui->TrayShow->Drop_Type >= 4)
    {
        num=6;
    }else{
        num=ui->TrayShow->Drop_Type+1;
    }
    ui->lcdNumber_droptype->display(num);
    ui->TrayShow->SetDropType(num);
}

void HomePage::on_Button_DropNum_Sub_clicked()
{
    int num;
    if(ui->TrayShow->Drop_Type == 6){
        num = 4;
    }else{
        num=ui->TrayShow->Drop_Type-1;
    }
    ui->lcdNumber_droptype->display(num);
    ui->TrayShow->SetDropType(num);
}
void HomePage::updateButtonStyles(int selectedButton)
{
    QString selectedStyle = "QpushButton{background-color: rgb(0, 0, 255);border: none;padding: 10px;border-radius: 5px;color:#ffffff;}";
    QString defaultStyle = "QpushButton{background-color: rgb(0, 85, 255);border: none;padding: 10px;border-radius: 5px;color:#ffffff;}";

    ui->StartSetingPage_BoardType1Button->setStyleSheet(selectedButton == 1 ? selectedStyle : defaultStyle);
    ui->StartSetingPage_BoardType2Button->setStyleSheet(selectedButton == 2 ? selectedStyle : defaultStyle);
    ui->StartSetingPage_BoardType3Button->setStyleSheet(selectedButton == 3 ? selectedStyle : defaultStyle);
}
void HomePage::on_StartSetingPage_BoardType1Button_clicked()
{
    ui->TrayShow->SetTrayType(1);
    ui->StartSetingPage_BoardType1Button->setStyleSheet("QPushButton{background-color: rgb(0, 85, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 14px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
    ui->StartSetingPage_BoardType2Button->setStyleSheet("QPushButton{background-color: rgb(0, 170, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 14px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
    ui->StartSetingPage_BoardType3Button->setStyleSheet("QPushButton{background-color: rgb(0, 170, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 14px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");

    Tray_Type = 1;
}

void HomePage::on_StartSetingPage_BoardType2Button_clicked()
{
    ui->TrayShow->SetTrayType(2);
    ui->StartSetingPage_BoardType1Button->setStyleSheet("QPushButton{background-color: rgb(0, 170, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 14px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
    ui->StartSetingPage_BoardType2Button->setStyleSheet("QPushButton{background-color: rgb(0, 85, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 14px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
    ui->StartSetingPage_BoardType3Button->setStyleSheet("QPushButton{background-color: rgb(0, 170, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 14px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
    Tray_Type = 2;
}


void HomePage::on_StartSetingPage_BoardType3Button_clicked()
{
    ui->TrayShow->SetTrayType(3);
    ui->StartSetingPage_BoardType1Button->setStyleSheet("QPushButton{background-color: rgb(0, 170, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 14px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
    ui->StartSetingPage_BoardType2Button->setStyleSheet("QPushButton{background-color: rgb(0, 170, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 14px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
    ui->StartSetingPage_BoardType3Button->setStyleSheet("QPushButton{background-color: rgb(0, 85, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 14px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");

    Tray_Type = 3;
}

void HomePage::on_ReturnToZeroButton_2_clicked()
{
    for(int i=0;i<12;i++)
    {
        send_data[i]=((char)data_return_Zero[i]);
    }
    serialPort->write(send_data,12);
}
//风扇
void HomePage::on_pushButton_3_clicked()
{
    if(FanSpeed_Flag)
    {
        FanSpeed_Flag=0;
        ui->pushButton_3->setStyleSheet("QPushButton{background-color: rgb(0, 170, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 24px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
    }
    else
    {
        FanSpeed_Flag=1;
        ui->pushButton_3->setStyleSheet("QPushButton{background-color: rgb(0, 85, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 24px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
    }
    qDebug() << "fan_speed"<<FanSpeed_Flag;
    data_fan[2]=fan_speed[FanSpeed_Flag*2];
    data_fan[3]=fan_speed[(FanSpeed_Flag*2)+1];

    for(int i=0;i<12;i++)
    {
        send_data[i]=((char)data_fan[i]);
    }
    serialPort->write(send_data,12);
}

void HomePage::on_pushButton_2_clicked()
{
    if(UV_lamp_flag)
    {
        qDebug() << "uv off";
        UV_lamp_flag=0;
        data_uvlamp[2]=0x00;
        for(int i=0;i<12;i++)
        {
            send_data[i]=((char)data_uvlamp[i]);
        }
        serialPort->write(send_data,12);
        ui->pushButton_2->setStyleSheet("QPushButton{background-color: rgb(0, 170, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 24px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
            }
    else
    {
        qDebug() << "uv on";
        UV_lamp_flag=1;
        data_uvlamp[2]=0x01;
        for(int i=0;i<12;i++)
        {
            send_data[i]=((char)data_uvlamp[i]);
        }
        serialPort->write(send_data,12);
        ui->pushButton_2->setStyleSheet("QPushButton{background-color: rgb(0, 85, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 24px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
            }
}

void HomePage::on_pushButton_led_clicked()
{
    if(LED_lamp_flag)
    {
        qDebug() << "LED off";
        LED_lamp_flag=0;
        data_LEDlamp[2]=0x00;
        for(int i=0;i<12;i++)
        {
            send_data[i]=((char)data_LEDlamp[i]);

        }
        serialPort->write(send_data,12);
        ui->pushButton_led->setStyleSheet("QPushButton{background-color: rgb(0, 170, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 24px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");

    }
    else
    {
        qDebug() << "LED on";
        LED_lamp_flag=1;
        data_LEDlamp[2]=0x01;
        for(int i=0;i<12;i++)
        {
            send_data[i]=((char)data_LEDlamp[i]);
        }
        serialPort->write(send_data,12);
        ui->pushButton_led->setStyleSheet("QPushButton{background-color: rgb(0, 85, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 24px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
        }
}

void HomePage::on_heatButton_clicked()
{
    if(HeatBoard_flag)
    {
        qDebug() << "heatboard off";
        HeatBoard_flag=0;
        for(int i=0;i<12;i++)
        {
            send_data[i]=((char)data_openheatborad[i]);
        }
        serialPort->write(send_data,12);
        ui->heatButton->setStyleSheet("QPushButton{background-color: rgb(0, 170, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 24px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");

    }
    else
    {
        qDebug() << "heatboard on";
        HeatBoard_flag=1;
        for(int i=0;i<12;i++)
        {
            send_data[i]=((char)data_offheatborad[i]);
        }
        serialPort->write(send_data,12);
        ui->heatButton->setStyleSheet("QPushButton{background-color: rgb(0, 85, 255);border-style: outset;border-width: 2px;border-radius:5px;border-color: #8B7355;font: bold 24px;min-width:2em;color:white;font-family:华文新魏;padding: 5px;}");
            }
}

void HomePage::on_pushButton_go_mainpage_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->StackedWidget_HomePage);
}
//滴加
void HomePage::on_StartSetingPage_NextButton_clicked()
{
    // ui->StartSetingPage_BoardType1Button->setStyleSheet("QpushButton{background-color:#00ff00;border: none;padding: 10px;border-radius: 5px;color:#ffffff;}");
    // ui->StartSetingPage_BoardType2Button->setStyleSheet("QpushButton{background-color:#00ff00;border: none;padding: 10px;border-radius: 5px;color:#ffffff;}");
    // ui->StartSetingPage_BoardType3Button->setStyleSheet("QpushButton{background-color:#00ff00;border: none;padding: 10px;border-radius: 5px;color:#ffffff;}");

    int num=ui->TrayShow->DropNum;
    int dorp_type=ui->TrayShow->Drop_Type;
    int drop_area = ui->TrayShow->DropArea;
    int tray_type = Tray_Type;
    if((num>0)&&(num<=25))
    {
        unsigned char dropnum = num;
        unsigned char droptype = dorp_type;
        unsigned char droparea = drop_area;
        unsigned char traytype = tray_type;
        data_work[1]=droptype;
        data_work[2]=dropnum;
        data_work[3]=traytype;
        data_work[4]=droparea;
        for(int i=0;i<12;i++)
        {
            send_data[i]=((char)data_work[i]);
        }
        serialPort->write(send_data,12);
        qDebug()<<"drop type"<<dorp_type<< "drop num"<<num<< "drop area"<<drop_area<< "tray type"<<tray_type;
    }
}



